/*******************************************************************************
 * Copyright 2011-2012 Dik Grapendaal
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package sh.grapendaal.tsuushin.message;

import sh.grapendaal.tsuushin.tracking.User;

public abstract class IncomingMessage {
	private String raw;
	private String[] chunks;
	private MessageType messageType;
	private User user;
	private String content;

	public IncomingMessage() {
	}

	/**
	 * This constructor must be called from extending classes. It sets the
	 * message type of this IncomingMessage instance.
	 * 
	 * @param messageType
	 *            The message type this instance represents.
	 */
	protected IncomingMessage(MessageType messageType) {
		this.messageType = messageType;
	}

	/**
	 * Returns the raw message this IncomingMessage instance represents.
	 * 
	 * @return The raw message as it was received.
	 */
	public String getRaw() {
		return raw;
	}

	/**
	 * Returns the array with the raw message split by spaces. Useful for
	 * extracting data out of responses.
	 * 
	 * @return The raw message split by spaces.
	 */
	public String[] getChunks() {
		return chunks;
	}

	/**
	 * Returns the type of this IncomingMessage instance.
	 * 
	 * @return The message type.
	 */
	public MessageType getMessageType() {
		return messageType;
	}

	/**
	 * Sets the message type of this IncomingMessage instance. This method is
	 * provided for messages that determine their exact type during parsing.
	 * 
	 * @param messageType
	 *            The message type to set.
	 */
	protected void setMessageType(MessageType messageType) {
		this.messageType = messageType;
	}

	/**
	 * Returns the User object associated with this message which contains
	 * information about the user who is responsible for this message.
	 * 
	 * @return The User object.
	 */
	public User getUser() {
		return user;
	}

	/**
	 * Returns the content of this message.
	 * 
	 * @return The content of the message.
	 */
	public String getContent() {
		return content;
	}

	/**
	 * Parses the given message for useful information and stores it in the
	 * class. Will try to parse the first chunk of a received IRC message into a
	 * User object, but also look if there's actual content in this message. If
	 * so, stores that in the content property.
	 * 
	 * @param message
	 *            The received message to parse.
	 */
	public void parse(String message) {
		raw = message;
		chunks = raw.split(" ");

		if (message.charAt(0) == ':') {
			String userString = chunks[0].substring(1);
			user = User.parse(userString);
		}

		int contentPosition = message.indexOf(" :");
		if (contentPosition != -1) {
			content = message.substring(contentPosition + 2);
		}
	}
}
